home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 05.zip / BS1 part 5 / IM_3.adf / Exec / piarc.LZH / pmbc.rexx < prev    next >
OS/2 REXX Batch file  |  1992-02-29  |  10KB  |  378 lines

  1. /*
  2.  * PMBC.rexx
  3.  *
  4.  *  Written by: Ben Williams
  5.  * Last Update: March 4th, 1992
  6.  *    Revision: 1.05
  7.  *         For: Black Belt Systems image processing series IM, IM F/c, and IP.
  8.  */
  9. parse arg var1
  10. /*
  11.  * open rexxsupport.library -- needed for some functions
  12.  */
  13. if ~show('L',"rexxsupport.library") then do
  14.   if addlib('rexxsupport.library',0,-30,0) then do
  15.       /* everything's ok */
  16.     end;
  17.   else do
  18.     say 'We Have A Library Problem, Unable To Load "rexxsupport.library"';
  19.     say 'Cannot operate PMBC.rexx without this library - sorry!';
  20.     exit 10;
  21.     end;
  22.   end;
  23. /*
  24.  * This will automatically direct the script to the proper
  25.  * software, if it is running. No matter where the script is
  26.  * launched from. :^) I sure do like ARexx. :^))
  27.  */
  28. prtnme = 'IP_Port'; /* assume Image Professional */
  29. if show('P','IP_Port') = 0 then do
  30.   if show('P','IM_Port') = 0 then do
  31.     say "Can't find image processor's ARexx port!!!"; /* not running? */
  32.     say "This script requires IP, IM or IM F/c to run!";
  33.     exit(20);
  34.     end;
  35.   else do
  36.     prtnme = 'IM_Port'; /* That's the thing about assumptions... */
  37.     end;                 /* We make em, user's break em.          */
  38.   end;
  39.  
  40.   /*
  41.    * This code attempts to read a file called "picmdpath" from REXX:
  42.    * If it can't find it, the script will assume that the commands
  43.    * associated with this PI Module are in "c:". If the file exists,
  44.    * the script will look in the path that is specified in the file.
  45.    * If you create this file, you MUST put a complete, correct path
  46.    * in it; if the commands are in a sub-directory, you have to put
  47.    * the trailing slash on the path (like, device:dir/).
  48.    * 
  49.    */
  50.   cmdpath = 'c:';
  51.   if open(fhandle,'rexx:picmdpath','read') then  /* open the file */
  52.     do
  53.       cmdpath = readln(fhandle);
  54.       call close(fhandle);  /* close the file    */
  55.     end
  56.  
  57. /*
  58.  * (Possibly) prompt user - load, or save?
  59.  */
  60. if var1 = 'load' then do
  61.   pick = 1
  62.   end
  63. else if var1 = 'save' then do
  64.   pick = 2
  65.   end
  66. else do
  67.   address(prtnme);
  68.   options results;
  69.   'gadgets "Load","PMBC","Save","PMBC"';
  70.   pick = result;
  71.   options;
  72.   address;
  73.   end;
  74.  
  75. if pick=0 then do
  76.   exit 0;
  77.   end;
  78.  
  79.   options results;
  80.   'gadgets "Show PMBC","Progress","Quiet",""';
  81.   pmbcprogress = result-1;
  82.   options;
  83.   if pmbcprogress < 0 then do
  84.     address;
  85.     'tofront';
  86.     exit 0;
  87.     end
  88.  
  89. /*
  90.  * PI driver for compressor
  91.  */
  92. if pick=2 then do /* compression */
  93.  
  94.  
  95.   bufname = 'image';
  96.   strn = ' -q ';
  97.   address(prtnme);
  98.  
  99.   prevpath = 'ram:'; /* put user in ram to start with... */
  100.   if show('C',pmbcpath) = 1 then do
  101.     prevpath = getclip(pmbcpath);
  102.     end;
  103.  
  104.   options results;
  105.   'current';
  106.   bufdata = result; /* get name of buffer, if there is one */
  107.   parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
  108.   if bname ~= '<none>' then do
  109.     bufname = bname;
  110.     end;
  111.   if (length(bufname) > 4) then do
  112.     epos = pos('.pmbc',bufname,length(bufname)-4);
  113.     if epos ~= 0 then do
  114.       bufname = left(bufname,epos-1)
  115.       end
  116.     end;
  117.   'filerequest "'||prevpath||'","'||bufname||'",".pmbc","Save PMBC"';
  118.   pmbcfile = result;
  119.   options;
  120.  
  121.   if pmbcfile = 'FR_CANCELLED' then do
  122.     address(prtnme);
  123.     'imtofront';
  124.     exit 0;
  125.     end;
  126.  
  127. call mungefilename(); /* make filename complete path */
  128. thispath = gimmepath(pmbcfile);
  129. call setclip(pmbcpath,thispath);
  130.  
  131.   address(prtnme);
  132.   options results;
  133.   'jackin';
  134.   jackadr = result;
  135.   options;
  136.  
  137.   'wbtofront';
  138.   if pmbcprogress = 0 then do
  139.     address command cmdpath||'wrpmbc -s -c -o "'||pmbcfile||'" -j '||jackadr;
  140.     end;
  141.   else do
  142.     address command cmdpath||'wrpmbc -o "'||pmbcfile||'" -j '||jackadr;
  143.     end;
  144.  
  145.   address(prtnme);
  146.   'imtofront';
  147.   address;
  148.  
  149.   exit 0;
  150.  
  151.   end;
  152.  
  153. else do /* decompression */
  154.  
  155. /*
  156.  * Setup default path
  157.  */
  158.   prevpath = 'ram:'; /* put user in ram to start with... */
  159. /*
  160.  * Now, get old path if it exists
  161.  */
  162.   if show('C',pmbcpath) = 1 then do
  163.     prevpath = getclip(pmbcpath);
  164.     end;
  165.  
  166.   address(prtnme);
  167.   options results;
  168.   'current';
  169.   bufdata = result; /* get name of buffer, if there is one */
  170.   parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum;
  171.   if bname ~= '<none>' then do
  172.     bufname = bname;
  173.     end;
  174.   if (length(bufname) > 4) then do
  175.     epos = pos('.pmbc',bufname,length(bufname)-4);
  176.     if epos ~= 0 then do
  177.       bufname = left(bufname,epos-1)
  178.       end
  179.     end;
  180.   'filerequest "'||prevpath||'","'||bufname||'",".pmbc","Load PMBC"';
  181.   pmbcfile = result;
  182.   options;
  183.  
  184.   if pmbcfile = 'FR_CANCELLED' then do
  185.     address(prtnme);
  186.     'imtofront';
  187.     exit 0;
  188.     end;
  189.  
  190.   call mungefilename();
  191.   thispath = gimmepath(pmbcfile);
  192.   call setclip(pmbcpath,thispath);
  193.  
  194.   fileinfo = statef(pmbcfile);
  195.   parse var fileinfo fitype fibytes fiblocks fiflags fidays fimins fiticks ficomment;
  196.  
  197.   if fitype = '' then do
  198.     'message Cannot locate "'pmbcfile'" for processing';
  199.     exit 10;
  200.     end;
  201.  
  202.   if fitype = 'DIR' then do
  203.     'message Must specify a file, not a directory';
  204.     exit 10;
  205.     end;
  206.  
  207. /*
  208.  * at this point, we have at least some assurance that we
  209.  * have a real pmbc file to work with. Now, we need to look into
  210.  * the file and see how big the image is, so we can open a new
  211.  * buffer of the appropriate size.
  212.  */
  213.  
  214.   call open(fhandle,pmbcfile,'read');    /* open the file     */
  215.   offset = seek(fhandle,0,'B');          /* go to PMBC id     */
  216.   filecode = readch(fhandle,4);          /* read in 'PMBC'    */
  217.  
  218.   offset = seek(fhandle,4,'B');          /* go to x dim       */
  219.   width = c2d(readch(fhandle,1)) * 256;
  220.   width = width + c2d(readch(fhandle,1));
  221.  
  222.   offset = seek(fhandle,6,'B');          /* go to y dim       */
  223.   height = c2d(readch(fhandle,1)) * 256;
  224.   height = height + c2d(readch(fhandle,1));
  225.  
  226.   call close(fhandle);                   /* close the file    */
  227.  
  228.   masked = 'NO';
  229.   
  230.   if filecode ~== 'PMBC' then do
  231.     if filecode ~== 'PmBC' then do
  232.       "message This is not a PMBC file!";
  233.       exit 10;
  234.       end;
  235.     else do
  236.       masked = 'YES';
  237.       end;
  238.     end;
  239.  
  240.   if height < 0 then do
  241.     "message Bad height: "||height;
  242.     exit 0;
  243.     end;
  244.  
  245.   if height > 32767 then do
  246.     "message Bad height: "||height;
  247.     exit 0;
  248.     end;
  249.  
  250.   if width < 0 then do
  251.     "message Bad width: "||width;
  252.     exit 0;
  253.     end;
  254.  
  255.   if width > 32767 then do
  256.     "message Bad width: "||width;
  257.     exit 0;
  258.     end;
  259.  
  260.   address(prtnme);
  261.   
  262.   'imtofront'; /* show user the IM screen */
  263.   /* is there already a primary buffer??? */
  264.   options results;
  265.   'current';
  266.   bufdata = result;
  267.   options;
  268.  
  269.   parse var bufdata bname ',' bnum ',' bx ',' by ',' btot ',' bmem ',' bparname ',' bparnum
  270.   if bname ~= '<none>' then do
  271.     address(prtnme);
  272.   options results;
  273.   'askyn '||'"Replace Primary ['||bname||']" "New As Primary"'
  274.   prefs = result;
  275.   options;
  276.   'autoredraw 0'; /* we do NOT want automatic redrawing */
  277.   address;
  278.   if prefs = 0 then do
  279.     address(prtnme);
  280.     'killbuff '||bnum; /* this kills the Primary Buffer */
  281.     address;
  282.     end;
  283.   end;
  284.  
  285.   /* New buffer is created at current resolution */
  286.   address(prtnme);
  287.   options results;
  288.   if masked = 'NO' then do
  289.     'newbuf '||width||' '||height;
  290.     end;
  291.   else do
  292.     'newbuf "'||width||'","'||height||'","","MASK"';
  293.     end;
  294.   if rc ~= 0 then do
  295.     "message Can't allocate buffer!";
  296.     exit 0;
  297.     end;
  298.   bnum = result;
  299.   'newcurrent '||bnum;
  300.   'rename '||bnum||' 'gxname;
  301.   address;
  302.  
  303.   address(prtnme);
  304.   options results;
  305.   'jackin';
  306.   jackadr = result;
  307.   options;
  308.  
  309.   'wbtofront';
  310.   'lockimage '||bnum;
  311.   if pmbcprogress = 0 then do
  312.     address command cmdpath||'rdpmbc -s -c -j '||jackadr||' -i "'||pmbcfile||'"';
  313.     end;
  314.   else do
  315.     address command cmdpath||'rdpmbc  -j '||jackadr||' -i "'||pmbcfile||'"';
  316.     end;
  317.   'unlockimage '||bnum;
  318.  
  319.   address(prtnme);
  320.   'imtofront';
  321.   'autoredraw 1';
  322.   'redraw';
  323.   address;
  324.  
  325.   exit 0;
  326.  
  327.   end;
  328.  
  329. /*
  330.  * gimmepath
  331.  *
  332.  * This takes the provided argument and sucks the path out of it, then
  333.  * returns that path to the caller, sans file name.
  334.  */
  335. gimmepath:
  336.   arg fullnamegx;
  337.     tempgx = reverse(fullnamegx);
  338.     lengx = length(fullnamegx);   /* get length of string */
  339.     slashdex = index(tempgx,'/'); /* first occurance of '/' from right */
  340.     colondex = index(tempgx,':');  /* first occurance of ':' from right */
  341.     seploc = 0; /* assumes current dir, no path supplied */
  342.     if slashdex ~= 0 then do /* we assume we are in a DIR */
  343.       seploc = (lengx - slashdex)+1;
  344.       end;
  345.     else do
  346.       if colondex ~= 0 then do /* we assume we are on a device */
  347.         seploc = (lengx - colondex)+1;
  348.         end;
  349.       end;
  350.   gxname = substr(fullnamegx,seploc+1); /* if you ever need it */
  351.   gxpath = left(fullnamegx,seploc);
  352.   return(gxpath);
  353.  
  354. /*
  355.  * Since pmbc.rexx can't be expected to know where the CD of the user
  356.  * is when this cmd is invoked, we have to check the path the user
  357.  * provides - if it's not specified right from a root, then we have
  358.  * to make it a complete specification from the root. That way, the
  359.  * entire path is passed to pmbc.rexx. This is a very nice, generally
  360.  * useful routine for this purpose. Note that it goes after a global
  361.  * filename variable, and so could (should) be re-written to handle
  362.  * parameters.
  363.  */
  364. mungefilename:
  365.   if index(pmbcfile,':') = 0 then do
  366.     curdir = pragma(D);
  367.     if right(curdir,1) ~= ':' then do
  368.       if right(curdir,1) ~= '/' then do
  369.         if curdir ~= '' then do
  370.           curdir = curdir || '/';
  371.           end;
  372.         end;
  373.       end;
  374.     pmbcfile = curdir||pmbcfile;
  375.     end;
  376.   return;
  377.  
  378.